//
//  CompletionAccountInfoController.m
//  YunDongAdmin
//
//  Created by lqq on 2018/9/11.
//  Copyright © 2018年 cloudfighting.com. All rights reserved.
//



typedef NS_ENUM(NSInteger, CompletionAccountInfoCellType) {
    CompletionAccountInfoCellType_BrandName,//品牌名
    CompletionAccountInfoCellType_BrandCreator,//品牌创始人
    CompletionAccountInfoCellType_GymLogo,//门店Logo
    CompletionAccountInfoCellType_GymName,//门店名称
    CompletionAccountInfoCellType_GymContactInfo,//门店联系方式
    CompletionAccountInfoCellType_GymAddress,//门店地址
};

#import "CompletionAccountInfoController.h"
#import "CommonTableViewCell.h"
#import "M_Gym.h"
#import "EditInfoController.h"
#import "SearchGymAddressViewController.h"
#import "ZLPhotoActionSheet.h"
#import "I_Media.h"
#import "I_Gym.h"
#import "AppDelegate.h"
#import "MainTabBarController.h"

@interface CompletionAccountInfoController () <UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *tableHeaderView;
@property (weak, nonatomic) IBOutlet UIButton *enterBtn;
@property (nonatomic, strong) NSArray *dataSource;


@property (nonatomic, strong) NSString *brandName;
@property (nonatomic, strong) NSString *brandCreator;
@property (nonatomic, strong) M_Gym *gym;
@end

@implementation CompletionAccountInfoController

#pragma mark Class Method
+ (instancetype)controller
{
    CompletionAccountInfoController *vc = [UIStoryboard controllerWithStoryBoardIdentify:NSStringFromClass([CompletionAccountInfoController class]) inStoryBoard:@"Account"];
    return vc;
}

#pragma mark Life Circle
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self configUI];
    
    _gym = [[M_Gym alloc] init];
    _dataSource = @[@[@(CompletionAccountInfoCellType_BrandName),@(CompletionAccountInfoCellType_BrandCreator)],@[@(CompletionAccountInfoCellType_GymLogo)],@[@(CompletionAccountInfoCellType_GymName),@(CompletionAccountInfoCellType_GymContactInfo),@(CompletionAccountInfoCellType_GymAddress)]];
}

#pragma mark Config/UI
- (void)configUI
{
    self.tableView.backgroundColor = TP.controllerBackColor;
    self.tableView.tableFooterView = [UIView new];
    self.tableHeaderView.height = 132 + [Toolkit getStatusBarHeight];
    self.tableView.tableHeaderView = self.tableHeaderView;
    [self.tableView registerClass:[CommonTableViewCell class] forCellReuseIdentifier:NSStringFromClass([CommonTableViewCell class])];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    if ([_tableView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    
    self.enterBtn.layer.cornerRadius = 2;
    self.enterBtn.layer.masksToBounds = YES;
    [self.enterBtn setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"#3AA7FF" alpha:0.5] size:CGSizeMake(1, 1)] forState:UIControlStateDisabled];
    [self.enterBtn setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"#3AA7FF" alpha:1] size:CGSizeMake(1, 1)] forState:UIControlStateNormal];

    self.enterBtn.enabled = NO;
}

- (void)refreshUI
{
    if (_brandName == nil || _brandCreator == nil || _gym.gymLogo == nil || _gym.gymName == nil || _gym.gymContactinfo == nil || _gym.gymAddress
         == nil) {
        self.enterBtn.enabled = NO;
    } else {
        self.enterBtn.enabled = YES;
    }
    [self.tableView reloadData];
}

#pragma mark Action
- (IBAction)enterBtnClicked:(id)sender {
    [AV showLoading:@"请稍后"];
    WeakSelf(self);
    [I_Gym creatBrandAndGymWithBrandName:_brandName BrandCreator:_brandCreator gym:_gym success:^{
        [AV hideen];
        M_Account *account = RI.account;
        account.brandName = weakself.brandName;
        account.brandCreator = weakself.brandCreator;
        RI.account = account;
        AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
        MainTabBarController *mainTabBarVC = [[MainTabBarController alloc] init];
        delegate.window.rootViewController = mainTabBarVC;
    } failure:^(NSError *error) {
        [AV showError:error];
    }];
}

#pragma mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *list = _dataSource[section];
    return list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CommonTableViewCell class])];
    cell.showStyle = CommonTableViewShowStyleArrow_RightText;
    
    CompletionAccountInfoCellType cellType = [_dataSource[indexPath.section][indexPath.row] integerValue];
    switch (cellType) {
        case CompletionAccountInfoCellType_BrandName:
        {
            cell.leftLabel.text = @"健身房名称";
            cell.rightLabel.text = _brandName;
        }
            break;
        case CompletionAccountInfoCellType_BrandCreator:
        {
            cell.leftLabel.text = @"管理员名称";
            cell.rightLabel.text = _brandCreator;
        }
            break;
        case CompletionAccountInfoCellType_GymLogo:
        {
            CompletionAccountInfoLogoCell *logoCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CompletionAccountInfoLogoCell class])];
            [logoCell.gymLogoImageView setNetImageWithUrl:_gym.gymLogo];
            return logoCell;
        }
            break;
        case CompletionAccountInfoCellType_GymName:
        {
            cell.leftLabel.text = @"门店名称";
            cell.rightLabel.text = _gym.gymName;
        }
            break;
        case CompletionAccountInfoCellType_GymContactInfo:
        {
            cell.leftLabel.text = @"门店电话";
            cell.rightLabel.text = _gym.gymContactinfo;
        }
            break;
        case CompletionAccountInfoCellType_GymAddress:
        {
            cell.leftLabel.text = @"门店地址";
            cell.rightLabel.text = _gym.gymAddress;
        }
            break;
            
        default:
            break;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CompletionAccountInfoCellType cellType = [_dataSource[indexPath.section][indexPath.row] integerValue];
    switch (cellType) {
        case CompletionAccountInfoCellType_BrandName:
        {
            EditInfoController *editVC = [EditInfoController controllerWithEditType:EditInfoType_BrandName originStr:_brandName];
            WeakSelf(self);
            editVC.editBlock = ^(NSString *editStr) {
                weakself.brandName = editStr;
                [weakself refreshUI];
            };
            [self.navigationController pushViewController:editVC animated:YES];
        }
            break;
        case CompletionAccountInfoCellType_BrandCreator:
        {
            EditInfoController *editVC = [EditInfoController controllerWithEditType:EditInfoType_BrandCreator originStr:_brandCreator];
            WeakSelf(self);
            editVC.editBlock = ^(NSString *editStr) {
                weakself.brandCreator = editStr;
                [weakself refreshUI];
            };
            [self.navigationController pushViewController:editVC animated:YES];
        }
            break;
        case CompletionAccountInfoCellType_GymLogo:
        {
            ZLPhotoActionSheet *actionSheet = [[ZLPhotoActionSheet alloc] init];
            actionSheet.configuration.allowSelectGif = NO;
            actionSheet.configuration.allowSelectVideo = NO;
            actionSheet.configuration.allowSelectLivePhoto = NO;
            actionSheet.configuration.allowEditImage = YES;
            actionSheet.configuration.maxSelectCount = 1;
            actionSheet.configuration.navBarColor= HEXColor(@"#3AA7FF");
            actionSheet.configuration.navTitleColor = [UIColor whiteColor];
            actionSheet.configuration.bottomBtnsNormalTitleColor = HEXColor(@"3AA7FF");
            actionSheet.configuration.bottomBtnsDisableBgColor = RGBA(58, 167, 255, 0.5);
            actionSheet.configuration.editAfterSelectThumbnailImage = YES;
            actionSheet.configuration.clipRatios = @[GetClipRatio(1, 1)];
            actionSheet.sender = self;
            
            __weak typeof(self) weakSelf = self;
            [actionSheet setSelectImageBlock:^(NSArray<UIImage *> * _Nullable images, NSArray<PHAsset *> * _Nonnull assets, BOOL isOriginal) {
                [AV showLoading:@"正在上传"];
                [I_Media upLoadImages:images success:^(NSArray *urls) {
                    [AV hideen];
                    weakSelf.gym.gymLogo = urls.firstObject;
                    [weakSelf refreshUI];
                } failure:^(NSError *error) {
                    [AV showErrorMessage:@"照片上传失败"];
                }];
            }];
            [actionSheet showPhotoLibrary];
        }
            break;
        case CompletionAccountInfoCellType_GymName:
        {
            SearchGymAddressViewController *searchGYMVC = [SearchGymAddressViewController controllerWithSearchType:SearchGYMAddressType_SearchName];
            WeakSelf(self);
            searchGYMVC.choiceBlock = ^(M_Gym *searchGym) {
                weakself.gym.gymAddress = searchGym.gymAddress;
                weakself.gym.gymAddressId = searchGym.gymAddressId;
                weakself.gym.gymCity = searchGym.gymCity;
                weakself.gym.gymDistrict = searchGym.gymDistrict;
                weakself.gym.gymLocationLatitude = searchGym.gymLocationLatitude;
                weakself.gym.gymLocationLongitude = searchGym.gymLocationLongitude;
                weakself.gym.gymName = searchGym.gymName;
                weakself.gym.gymProvince = searchGym.gymProvince;
                [weakself refreshUI];
            };
            [self.navigationController pushViewController:searchGYMVC animated:YES];
        }
            break;
        case CompletionAccountInfoCellType_GymContactInfo:
        {
            EditInfoController *editVC = [EditInfoController controllerWithEditType:EditInfoType_GymMobile originStr:_gym.gymContactinfo];
            WeakSelf(self);
            editVC.editBlock = ^(NSString *editStr) {
                weakself.gym.gymContactinfo = editStr;
                [weakself refreshUI];
            };
            [self.navigationController pushViewController:editVC animated:YES];
        }
            break;
        case CompletionAccountInfoCellType_GymAddress:
        {
            SearchGymAddressViewController *searchGYMVC = [SearchGymAddressViewController controllerWithSearchType:SearchGYMAddressType_SearchAddress];
            WeakSelf(self);
            searchGYMVC.choiceBlock = ^(M_Gym *searchGym) {
                weakself.gym.gymAddress = searchGym.gymAddress;
                weakself.gym.gymAddressId = searchGym.gymAddressId;
                weakself.gym.gymCity = searchGym.gymCity;
                weakself.gym.gymDistrict = searchGym.gymDistrict;
                weakself.gym.gymLocationLatitude = searchGym.gymLocationLatitude;
                weakself.gym.gymLocationLongitude = searchGym.gymLocationLongitude;
                //                weakself.gym.gymName = searchGym.gymName;
                weakself.gym.gymProvince = searchGym.gymProvince;
                [weakself refreshUI];
            };
            [self.navigationController pushViewController:searchGYMVC animated:YES];
        }
            break;
            
        default:
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CompletionAccountInfoCellType cellType = [_dataSource[indexPath.section][indexPath.row] integerValue];
    if (cellType == CompletionAccountInfoCellType_GymLogo) {
        return 95;
    } else if (cellType == CompletionAccountInfoCellType_GymAddress) {
        CGFloat height = [_gym.gymAddress boundingRectWithSize:CGSizeMake((kDeviceWidth - 140 - 33), 300) font:RegularFont(14)].height;
        height += 28;
        if (height < 50) {
            height = 50;
        }
        return height;
    }
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.01;
    }
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}


@end


@implementation CompletionAccountInfoLogoCell


@end
